﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using Newtonsoft.Json;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.WcfService.Contracts;
using System.Data;

namespace JM.JCY.WcfService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“PhotoAlbum”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PhotoAlbum : IPhotoAlbum
    {
        public void UpdateMedia(string desc, int albumId, int mediaId, string userName, int staffId)
        {
            using (PhotoAlbumProxy accessProxy = new PhotoAlbumProxy())
            {
                PhotoAlbumDataSet albumDS = accessProxy.getAlbumDbyAlbumId(albumId);
                var albumRow = albumDS.T_Album[0];
                var albumContentRow = albumDS.T_AlbumContent.FindByAlbumContentId(mediaId);
                albumRow.LatestUpdateTime = DateTime.Now;
                albumRow.LAST_UPDATED_BY = userName;
                albumRow.LAST_UPDATED_TIME = DateTime.Now;
                albumRow.ThumbNailImgUrl = albumContentRow.ThumbNailImgUrl;

                albumContentRow.Description = desc;
                albumContentRow.LAST_UPDATED_TIME = DateTime.Now;
                albumContentRow.LAST_UPDATED_BY = userName;

                accessProxy.CreateAlbumwithUpload(albumDS);
            }
        }

        public string GetMediaDescriptionById(int mediaId)
        {
            using (PhotoAlbumProxy proxy = new PhotoAlbumProxy())
            {
                PhotoAlbumSearchDataSet.T_AlbumContentDataTable MediaDT = proxy.GetMadieById(mediaId);
                var result = from ret in MediaDT
                             select new
                             {
                                 MediaDescription = ret.Description,
                                 LastUpdatedTime = ret.LAST_UPDATED_TIME.ToString("yyyy-MM-dd HH:mm:ss"),
                                 objectId = ret.SharePointObjectId
                             };
                return JsonConvert.SerializeObject(result);
            }
        }

        public void LikeOfAlbum(int staffId, int objectId, string objectType)
        {
            using (PhotoAlbumProxy proxy = new PhotoAlbumProxy())
            {
                PhotoAlbumSearchDataSet ds = new PhotoAlbumSearchDataSet();
                PhotoAlbumSearchDataSet.T_LikeDataTable likeDt = ds.T_Like; ;
                PhotoAlbumSearchDataSet.T_LikeRow likeRow = likeDt.NewT_LikeRow();
                likeRow.StaffId = staffId;
                likeRow.LikeObjectId = objectId.ToString();
                likeRow.LikeObjectType = objectType;
                likeRow.CREATED_BY = staffId.ToString();
                likeRow.CREATED_TIME = DateTime.Now;
                likeRow.VERSION_NO = 1;
                likeRow.TRANSACTION_ID = "1";

                likeDt.AddT_LikeRow(likeRow);
                proxy.LikeOfAlbum(ds);
            }
        }

        public string GetLikes(int staffId, int objectId, string objectType)
        {
            using (PhotoAlbumProxy proxy = new PhotoAlbumProxy())
            {
                PhotoAlbumSearchDataSet.T_SearchLikeDataTable likes = proxy.GetLike(objectId, objectType);
             
                DataRow[] rows = likes.Select(string.Format("StaffId={0}", staffId));
                string JSON = "";
                if (rows.Length > 0) //if current user likes this object
                {
                  var result = from like in likes
                             where like.StaffId == staffId
                             select new
                             {
                                 count = likes.Count,
                                 staffId = like.StaffId,
                                 staffName = like.UserName
                             };
                    JSON = JsonConvert.SerializeObject(result);
                }
                else //current user didn't like this object
                {
                  var result = (from like in likes
                              select new
                              {
                                  count = likes.Count,
                                  staffId = like.StaffId,
                                  staffName = like.UserName
                              }).Take(1);
                    JSON = JsonConvert.SerializeObject(result);
                }

                return JSON;
            }
        }

    }
}
